package com.huicoo.forestmanager.ui.patrol.forestCampFragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.huicoo.forestmanager.R;
import com.huicoo.forestmanager.adapter.ForestCampAdapter;
import com.huicoo.forestmanager.base.BaseFragment;
import com.huicoo.forestmanager.network.bean.patrol.PatrolToDoList;
import com.huicoo.forestmanager.network.bean.patrol.PatrollingTasksinif;
import com.huicoo.forestmanager.others.TaskFinishEvent;
import com.huicoo.forestmanager.ui.patrol.forestCampFragment.contract.ForestCampFragmentContract;
import com.huicoo.forestmanager.ui.patrol.forestCampFragment.presenter.ForestCampFragmentPresenter;
import com.huicoo.forestmanager.ui.patrol.forestCampTask.ForestCampTaskActivity;
import com.huicoo.forestmanager.util.JsonUtils;
import com.huicoo.forestmanager.util.LogUtils;
import com.huicoo.forestmanager.util.toast.ToastUtils;
import com.huicoo.forestmanager.widget.LoadingDialog;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;

/**
 * --------- 日期 ------- 维护人 ------------ 变更内容 --------
 * 2019/4/5		    Administrator
 * <p>
 * 待巡林班片段
 */
public class ForestCampFragment extends BaseFragment implements ForestCampFragmentContract.View {


    @BindView(R.id.recycler_iew)
    RecyclerView recycler_iew;
    @BindView(R.id.spring_view)
    SpringView spring_view;

    LoadingDialog loadingDialog;
    private ForestCampAdapter mForestCampAdapter;


    public static ForestCampFragment getInstance() {
        return new ForestCampFragment();
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_forest_camp;
    }

    @Override
    protected void initView() {
        initSpringView();
        initRecyclerView();

        //初始化数据
        initData();

        EventBus.getDefault().register(this);
    }

    //初始化数据
    private void initData() {
        //初始化P层
        presenter = new ForestCampFragmentPresenter(this);
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(getActivity());
        }
        loadingDialog.show();
        requestData();
    }

    private void requestData() {
        //	获取待巡林班列表
        ((ForestCampFragmentPresenter) presenter).patrolToDoList();
    }

    private void initSpringView() {
        spring_view.setHeader(new DefaultHeader(getContext()));
//        spring_view.setFooter(new DefaultFooter(getContext()));
        spring_view.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                LogUtils.v("gogogo on refresh");
                requestData();
            }

            @Override
            public void onLoadmore() {

            }
        });

    }

    private void initRecyclerView() {
        //设置布局管理器
        recycler_iew.setLayoutManager(new GridLayoutManager(getContext(), 2));
        //创建适配器
        mForestCampAdapter = new ForestCampAdapter(null);
        //设置适配器
        recycler_iew.setAdapter(mForestCampAdapter);
        // 点击事件
        mForestCampAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                PatrollingTasksinif info = mForestCampAdapter.getData().get(position);
                Bundle bundle = new Bundle();
                bundle.putString("detail", JsonUtils.toJson(info));
                Intent intent = new Intent(getActivity(), ForestCampTaskActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    //	获取待巡林班列表  成功
    @Override
    public void patrolListSuccess(PatrolToDoList data) {
        List<PatrollingTasksinif> patrollingTasks = data.getData().patrollingTasks;
        mForestCampAdapter.setNewData(patrollingTasks);
        loadingDialog.dismiss();
        spring_view.onFinishFreshAndLoad(); //完成刷新和加载
    }

    //	获取待巡林班列表  失败
    @Override
    public void patrolListFail(String msg) {
        loadingDialog.dismiss();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void taskFinishEvent(TaskFinishEvent event) {
        LogUtils.v("gogogo get task finish event");
        if (loadingDialog != null) {
            loadingDialog.show();
        }
        requestData();
    }
}
